package com.mjsheng.news.splash.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

import com.mjsheng.news.R;
import com.mjsheng.news.Utils.Constant;
import com.mjsheng.news.Utils.JsonUtil;
import com.mjsheng.news.Utils.SharePrenceUtil;
import com.mjsheng.news.service.DownloadingService;
import com.mjsheng.news.splash.bean.Ads;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class SplashActivity extends AppCompatActivity {

    //json 缓存
    static final String JSON_CACHE = "ads_Json";
    static final String JSON_CACHE_TIME_OUT = "ads_Json_time_out";
    static final String JSON_CACHE_LAST_SUCCESS = "ads_Json_last";
    static final String LAST_IMAGE_INDEX ="img_index";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager
                .LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash);

        getAds();
    }

    public void getAds() {
        String cache = SharePrenceUtil.getString(this, JSON_CACHE);

        if (TextUtils.isEmpty(cache)) {//若缓存为空，请求网络
            httpRequest();
        } else {
            //获取超时时间
            int time_Out = SharePrenceUtil.getInt(this,JSON_CACHE_TIME_OUT);
            //现在时间
            long now  = System.currentTimeMillis();
            //当时请求时间
            long last = SharePrenceUtil.getLong(this,JSON_CACHE_LAST_SUCCESS);
            if ((now-last)>time_Out*60*1000) {//若超时，请求网络
                httpRequest();
            }
        }

    }

    public void httpRequest() {
        Log.i("SplashhttpRequest","httpRequest");
        final OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url(Constant.SPlASH_URL)
                .build();
        //开启异步请求
        client.newCall(request).enqueue(new Callback() {
            //请求失败
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
            }

            //请求成功
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (!response.isSuccessful())
                    throw new IOException("Unexpected code " + response);

                String data = response.body().string();
                //解析json
                Ads ads = JsonUtil.parseJson(data, Ads.class);
                if (null != ads) {
                    Log.i("SplashActivity", ads.toString());
                    //http成功后,缓存json
                    SharePrenceUtil.saveString(SplashActivity.this, JSON_CACHE, data);
                    //http成功后,缓存超时时间
                    SharePrenceUtil.saveInt(SplashActivity.this,JSON_CACHE_TIME_OUT,ads.getNext_req());
                    //http成功后,缓存上次请求成功的时间
                    SharePrenceUtil.saveLong(SplashActivity.this,JSON_CACHE_LAST_SUCCESS,System.currentTimeMillis());
                    //把解析后的数据传到DownloadingService下载
                    Intent intent = new Intent();
                    intent.setClass(SplashActivity.this, DownloadingService.class);
                    intent.putExtra(DownloadingService.ADS_DATE, ads);
                    startService(intent);
                }
                //                System.out.println(response.body().string());
            }
        });
    }

}
